package cn.sms.org.service.impl;

import cn.sms.basic.service.impl.BaseServiceImpl;
import cn.sms.basic.util.PageList;
import cn.sms.org.domain.Department;
import cn.sms.org.mapper.DepartmentMapper;
import cn.sms.org.query.DepartmentQuery;
import cn.sms.org.service.IDepartmentService;
import javafx.scene.Parent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements IDepartmentService {
    @Autowired
    private DepartmentMapper departmentMapper;

    @Override
    @Transactional
    public void add(Department department) {
        departmentMapper.save(department);
        this.update(department);
    }

    @Override
    @Transactional
    public void update(Department department) {
        String dirPath = "";
        if (department.getParent()==null){
            dirPath = "/"+department.getId();
        }else {
            Department parent = departmentMapper.loadById(department.getParent().getId());
            dirPath = parent.getDirPath()+"/"+department.getId();
        }
        department.setDirPath(dirPath);
        departmentMapper.update(department);
    }

    @Override
    public List<Department> queryTree() {
        return departmentMapper.queryTree();
    }


}
